let keyword = ""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isModalShow: false,
    blogList: [],
  },
  onLoad: function(options) {
    // 云服务器端获取数据库数据
    this._loadBlogList()
    // 小程序端获取数据库数据
    // let db = wx.cloud.database()
    // db.collection('blog').orderBy('creatTime', 'desc').get().then((res) => {
    //   let data = res.data
    //   for (let i = 0; i < data.length; i++) {
    //     data[i].creatTime = data[i].creatTime.toString()
    //   }
    //   this.setData({
    //     blogList: data
    //   })
    // })
  },
  onSearch(e) {
    this.setData({
      blogList: []
    })
    keyword = e.detail.searchWord
    this._loadBlogList()
  },
  // 获取博客列表
  _loadBlogList(start) {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'blog',
      data: {
        keyword,
        $url: 'list',
        start: start || 0,
        count: 10,
      }
    }).then((res) => {
      let blogList = this.data.blogList.concat(res.result)
      let tasks = []
      // 获取每条博客的评论数量
      for (let i = 0; i < blogList.length; i++) {
        let blogId = blogList[i]._id
        let promise = wx.cloud.callFunction({
          name: 'blog',
          data: {
            $url: 'detail',
            blogId
          },
        }).then((res) => {
          blogList[i].commentLength = res.result.blogComment.data.length
        })
        tasks.push(promise)
      }
      if (tasks.length == 0) {
        return
      }
      Promise.all(tasks).then((res) => {
        this.setData({
          // blogList: this.data.blogList.concat(res.result)
          blogList
        })
      })
      wx.hideLoading()
      wx.stopPullDownRefresh()
    })
  },
  onPublish() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: (res) => {
              this.loginSuccess({
                detail: res.userInfo
              })
            }
          })
        } else {
          this.setData({
            isModalShow: true
          })
        }
      }
    })

  },
  loginSuccess(e) {
    console.log(e)
    let userinfo = e.detail
    wx.navigateTo({
      url: `../blog-edit/blog-edit?nickName=${userinfo.nickName}&avatarUrl=${userinfo.avatarUrl}`,
    })
  },
  loginFail() {
    wx.showModal({
      title: '提示',
      content: '需要授权才能发布',
      showCancel: false,
      confirmColor: '#cc0000'
    })
  },
  onShow: function() {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      blogList: []
    })
    this._loadBlogList()
  },
  goComment(e) {
    wx.navigateTo({
      url: '../blog-comment/blog-comment?blogId=' + e.target.dataset.blogid,
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this._loadBlogList(this.data.blogList.length)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(e) {
    let blog = e.target.dataset.blog
    return {
      title: blog.content,
      path: `/pages/blog-comment/blog-comment?blogId=${blog._id}`
    }
  }
})